let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content:'',
    images: []
  },

   /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let project = wx.getStorageSync('projectInfo');
    if(project){
      // wx.setNavigationBarTitle({ title: project.title });
      this.setData({ project });
    }else{
      wx.navigateBack();
    }
  },

  /** 输入点评 */
  inputArea(e){
    this.setData({ content: e.detail.value });
  },

  /** 取消 */
  onCancel(){
    wx.navigateBack();
  },

  /** 提交 */
  onSumit(){
    if(this.data.content == ''){
      app.showFailToast('请填写点评内容');
      return false;
    }
    let params = {
      pro_id: this.data.project.id,
      content: this.data.content,
      images: this.data.images.join(';')
    };
    // console.log(params)
    app.request('post', '/api/Project/commentApi', params).then(res => {
      app.showSuccessToast('发布成功');
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/projectList/projectList',
        });
      }, 1500);
    }).catch(err => console.log(err));
  },

  /** 选择上传图片 */
  onChoosePic() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: `${app.globalData.rootUrl}/api/upload/upload`,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {'appid': app.appid},
          success: res => {
            let response = JSON.parse(res.data);
            if(res.statusCode == 200){
              if(response.code == 0){
                let images = that.data.images;
                images.push(response.thumb);
                that.setData({ images });
              }else{
                app.showFailToast(response.msg);
              }
            }else app.showFailToast('网络错误，请稍后再试');
          }
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})